import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Lily
 * Date: 2024-09-23
 * Time: 19:57
 */
public class test_3 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            int n = in.nextInt();
            String str1 = in.next();
            String str2 = in.next();

            if (str1==null||str2==null||str1.equals(str2)){
                System.out.println(-1);
            }
            String[] strs=new String[n];

            for (int i = 0; i < n; i++) {
                strs[i]=in.next();
            }

            int ret=Integer.MAX_VALUE;

            int left=-1;

            int right=200001;

            for (int i = 0; i < n; i++) {
                if (strs[i].equals(str1)){
                    left=i;
                }else if (strs[i].equals(str2)){
                    right=i;
                }
                ret=Math.min(Math.abs(right-left),ret);


            }

            if (left==-1||right==200001){
                System.out.println(-1);
            }else {
                System.out.println(ret);
            }

        }
    }
}
